package org.cnc.explain.lifecycle;

import org.cnc.explain.lifecycle.beandestroy_10.BeanDestroyBean;
import org.cnc.explain.lifecycle.beandestroy_10.BeanDestroyConfiguration;
import org.cnc.explain.lifecycle.beandestroy_10.DestructionAwareBeanPostProcessorBean;
import org.junit.jupiter.api.Test;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * <p>
 * #TODO: desc
 * </p>
 *
 * @author zhiyong.tan
 * @since 2022-09-04
 */
public class Step10BeanDestroy {
	@Test
	public void testDestroyBean() {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.refresh();
		ctx.register(BeanDestroyBean.class);
		ctx.getBean(BeanDestroyBean.class);
		ctx.close();
	}


	@Test
	public void testDestructionAwareBeanPostProcessor() {
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
		ctx.refresh();
		ctx.getBeanFactory().addBeanPostProcessor(new DestructionAwareBeanPostProcessor() {
			@Override
			public void postProcessBeforeDestruction(Object bean, String beanName) throws BeansException {
				if (DestructionAwareBeanPostProcessorBean.class.equals(bean.getClass())) {
					System.out.println("执行 DestructionAwareBeanPostProcessorBean 的销毁");
				}
			}
		});
		ctx.register(DestructionAwareBeanPostProcessorBean.class);
		ctx.getBean(DestructionAwareBeanPostProcessorBean.class);
		ctx.close();
	}
	@Test
	public void testCustomDestroyMethod(){
		AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(BeanDestroyConfiguration.class);;
		ctx.close();
	}


}
